package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.handler.IItemHandler;
import labox.innovation.gameserver.handler.ItemHandler;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.L2Armor;
import labox.innovation.gameserver.templates.item.L2ArmorType;
import labox.innovation.gameserver.templates.item.L2Item;
import labox.innovation.gameserver.templates.item.L2Weapon;
import labox.innovation.gameserver.templates.item.L2WeaponType;

/**
 *  物品的使用，  包括装备物品， 和使用物品
 * @author zjj
 *
 */
public final class UseItem extends L2GameClientPacket {
	private static final Logger _log = LoggerFactory.getLogger(UseItem.class.getName());

	private static final String _C__72_USEITEM = "[C] 0x72 UseItem";

	private long _objectId;

	/** Weapon Equip Task */
	public class WeaponEquipTask implements Runnable {
		L2ItemInstance item;

		FuzePcInstance activeChar;

		public WeaponEquipTask(L2ItemInstance it, FuzePcInstance character) {
			item = it;
			activeChar = character;
		}

		public void run() {
			//If character is still engaged in strike we should not change weapon
			if (activeChar.isAttackingNow())
				return;
			// Equip or unEquip
			activeChar.useEquippableItem(item, false);
		}

	}

	@Override
	protected void readImpl() {
		_objectId = readLong();
		// TODO:
		//readD();
	}

	@Override
	protected void runImpl() {

		FuzePcInstance activeChar = getClient().getActiveChar();
		if (activeChar == null)
			return;

		// Flood protect UseItem
		if (!activeChar.getFloodProtectors().getUseItem().tryPerformAction("use item"))
			return;

		L2ItemInstance item = activeChar.getInventory().getItemByObjectId(_objectId);
		if (item == null)
			return;

		if (item.isWear()) {
			// No unequipping wear-items
			return;
		}
		if (item.getItem().getType2() == L2Item.TYPE2_QUEST) {
			SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_USE_QUEST_ITEMS);
			activeChar.sendPacket(sm);
			sm = null;
			return;
		}

		// Char cannot use item when dead
		if (activeChar.isDead()) {
			SystemMessage sm = new SystemMessage(SystemMessageId.S1_CANNOT_BE_USED);
			sm.addItemName(item);
			getClient().getActiveChar().sendPacket(sm);
			sm = null;
			return;
		}

		// Char cannot use pet items
		if ((item.getItem() instanceof L2Armor && item.getItem().getItemType() == L2ArmorType.PET) || (item.getItem() instanceof L2Weapon && item.getItem().getItemType() == L2WeaponType.PET)) {
			SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_EQUIP_PET_ITEM); // You cannot equip a pet item.
			sm.addItemName(item);
			getClient().getActiveChar().sendPacket(sm);
			sm = null;
			return;
		}

		if (_log.isDebugEnabled())
			_log.debug(activeChar.getObjectId() + ": use item " + _objectId);

		if (!item.isEquipped()) {
			if (!item.getItem().checkCondition(activeChar, activeChar, true))
				return;
		}

		//如果是可装备的物品
		if (item.isEquipable()) {
			// No unequipping/equipping while the player is in special conditions
			if (activeChar.isStunned() || activeChar.isSleeping() || activeChar.isParalyzed()) {
				activeChar.sendMessage("Your status does not allow you to do that.");
				return;
			}

			switch (item.getItem().getBodyPart()) {

				case L2Item.SLOT_HAND: //武器
				{
					// prevent players to equip weapon while wearing combat flag
					if (activeChar.getActiveWeaponItem() != null && activeChar.getActiveWeaponItem().getItemId() == 9819) {
						activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
						return;
					}
					if (activeChar.isMounted()) {
						activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
						return;
					}
					if (activeChar.isDisarmed()) {
						activeChar.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
						return;
					}
	
					break;
				}
				
				case L2Item.SLOT_CHEST:
				case L2Item.SLOT_BELT:
				case L2Item.SLOT_L_BRACELET:
				case L2Item.SLOT_L_FINGER:
				case L2Item.SLOT_R_BRACELET:
				case L2Item.SLOT_R_FINGER:
				case L2Item.SLOT_SHOULDER:
				case L2Item.SLOT_STRIDER:
				case L2Item.SLOT_WING:
				case L2Item.SLOT_FEET:
				case L2Item.SLOT_HEAD:
				case L2Item.SLOT_LEGS:
				case L2Item.SLOT_DECO: {
	
					break;
				}
			}

			activeChar.useEquippableItem(item, true);

		}//
		//不可装备的物品
		else {

			if( !item.getItem().getCanuse() ){
				return;
			}
		
			IItemHandler handler = ItemHandler.getInstance().getItemHandler(item.getEtcItem());
			if (handler == null) {
				_log.warn("No item handler registered for item ID " + item.getItemId() + ".", Config.SERVER_ID);
			} else {
				handler.useItem(activeChar, item);
			}

		}//
	
	}

	@Override
	public String getType() {
		return _C__72_USEITEM;
	}
}
